package sy.dao.impl;

import java.util.Date;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import sy.dao.IotParkingOutExceptionDaoI;
import sy.model.IotParkingOutException;
import sy.model.WYAccountProjectMapping;
import sy.util.CodeUtil;
import sy.util.DateUtil;
@Repository("iotParkingOutExceptionDao")
public class IotParkingOutExceptionDaoImpl implements IotParkingOutExceptionDaoI{
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}
	
	@Autowired
	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	private Session getCurrentSession(){
		return this.getSessionFactory().getCurrentSession();
	}

	@Override
	public void saveList(List<IotParkingOutException> list) {
		// TODO Auto-generated method stub
		Session session = this.getCurrentSession();
		IotParkingOutException parkingOutException = null;
		for(int i = 0; i<list.size(); i++){
			parkingOutException = list.get(i);
			session.save(parkingOutException);
			if (i % 10 == 0) {  
				session.flush();  
				session.clear();  
			}  
		}
	}

	@Override
	public IotParkingOutException getByOutExceptionId(String outExceptionId) {
		// TODO Auto-generated method stub
		String hql = "from IotParkingOutException where outExceptionId = :outExceptionId";
		Query query = this.getCurrentSession().createQuery(hql);		
		query.setString("outExceptionId", outExceptionId);	
		List<IotParkingOutException> list = query.list();
		if(!list.isEmpty()){
			return list.get(0);
		}
		return null;
	}

	@Override
	public void modify(IotParkingOutException iotParkingOutException) {
		// TODO Auto-generated method stub
		this.getCurrentSession().update(iotParkingOutException);
	}

	@Override
	public List<IotParkingOutException> list() {
		// TODO Auto-generated method stub
		return this.getCurrentSession().createQuery("from IotParkingOutException order by status, outDate desc").list();
	}

	@Override
	public void updateDate() {
		// TODO Auto-generated method stub
		String hql = "update IotParkingOutException set date = :date, inDate = :inDate, outDate = :outDate, uploadTime = :uploadTime";
		Date date = DateUtil.getCurrentDateDate();
		Date inDate = DateUtil.formatDate(DateUtil.getCurrentDate()+" 09:20:00");
		Date outDate = DateUtil.formatDate(DateUtil.getCurrentDate()+" 20:50:00");
		Date uploadTime = new Date();
		Query query = this.getCurrentSession().createQuery(hql);
		query.setParameter("date", date);
		query.setParameter("inDate", inDate);
		query.setParameter("outDate", outDate);
		query.setParameter("uploadTime", uploadTime);
		query.executeUpdate();
	}

}
